﻿@model LogManager.ViewModels.FilterIndexViewModel
@using MvcContrib.UI.Grid;
@using MvcContrib.Pagination;
@using MvcContrib.UI.Pager;
@using LogManager.Helpers

@{
    ViewBag.Title = "Filter events";
}


@section Foot {
    <script type="text/javascript">
        jQuery(document).ready(function () {
            var dateFormat = 'dd.mm.yy';
            var eventType = $('#EventTypeFilter_Type');
            var filterName = $('#EventTypeFilter_Property');

            var serverName = $('#SourceFilter_ServerName');
            var logName = $('#SourceFilter_LogName');

            $('#DateFilter_Start').datepicker({ dateFormat: dateFormat });
            $('#DateFilter_End').datepicker({ dateFormat: dateFormat });

            eventType.change(function () {
                filterName.html('');
                if (eventType.val() !== null && eventType.val() !== 'All') {
                    $.getJSON('@(Url.Action("FilterNamesData"))' + '?typeName=' + eventType.val(), function (data) {
                        $.each(data, function (key, val) {
                            filterName.append('<option value="' + val + '" >' + val + '</option>');
                        });
                    });
                }
            });

            serverName.change(function () {
                logName.html('');
                if (serverName.val() !== null && serverName.val() !== 'All') {
                    $.getJSON('@(Url.Action("LogNamesData"))' + '?serverName=' + serverName.val(), function (data) {
                        $.each(data, function (key, val) {
                            logName.append('<option value="' + val + '" >' + val + '</option>');
                        });
                    });
                }
            });

            $('#doFilter').click(function (e) {
                e.preventDefault();
                //show loader
                $('#load-placeholder').html('<p id="load-placeholder">Importing and loading requested data.<br />Because the data imports asynchronously you won\'t see results immediately if it is the first time access.<br />Refresh at a later time to view the actual results.<br /><img src="@Url.Content("~/Content/Images/ajax-loader.gif")" alt="Loading" /><img src="@Url.Content("~/Content/Images/import.png")" alt="Loading" /></p>');

                var model = {
                    start: $('#DateFilter_Start').val(),
                    end: $('#DateFilter_End').val(),
                    logName: $('#SourceFilter_LogName').val(),
                    serverName: $('#SourceFilter_ServerName').val(),
                    user: $('#EventFilter_User').val(),
                    level: $('#EventFilter_Level').val(),
                    type: $('#EventTypeFilter_Type').val(),
                    property: $('#EventTypeFilter_Property').val(),
                    searchValue: $('#EventTypeFilter_SearchValue').val(),
                    view: $('#View').val()
                };

                for (var prop in model) {
                    if (model[prop] === "All" || model[prop] === null || model[prop] === "" || model[prop] == "Undefined") {
                        delete model[prop];
                    }
                }
                
                $.get('@(Url.Action("List","Event"))' + '?' + $.param(model), function (html) {
                    $('#load-placeholder').html(html);
                });
            });


        });
    </script>    
}

<h2>Filter events</h2>

<div id="search-form" class="group">
    @using (Html.BeginForm())
    { 
        <fieldset class="filter">
            <legend><h3>Date Filter</h3></legend>
            @Html.DisplayFor(m => m.DateFilter)
        </fieldset>
        
        <fieldset class="filter">
            <legend>
                <h3>Event Filter</h3>
            </legend>
            @Html.DisplayFor(m => m.EventFilter)
        </fieldset>
        
        <fieldset class="filter">
            <legend>
                <h3>Event Source Filter</h3>
            </legend>
            @Html.DisplayFor(m => m.SourceFilter)
        </fieldset>
        
        <fieldset class="filter advanced">
            <legend>
                <h3>Event Type Filter</h3>
            </legend>
            @Html.DisplayFor(m => m.EventTypeFilter)        
        </fieldset>
        <fieldset class="filter">
            <legend>
                <h3>Result View</h3>
            </legend>
            @Html.DropDownListFor(m => m.View,new SelectList(Model.Views))
        </fieldset>        
        <br clear="all" />
        <div>
            <input id="doFilter" type="submit" value="Filter" />
        </div>    
    }

    <fieldset>
    <legend><h3>Result</h3></legend>
    <p id="load-placeholder">
        Nothing searched yet...                
    </p>
</fieldset>
</div>